package cn.iocoder.yudao.module.lfd.controller.admin.banner;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.lfd.controller.admin.banner.vo.*;
import cn.iocoder.yudao.module.lfd.convert.banner.LfdBannerConvert;
import cn.iocoder.yudao.module.lfd.dal.dataobject.banner.LfdBannerDO;
import cn.iocoder.yudao.module.lfd.service.banner.LfdBannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - Banner")
@RestController
@RequestMapping("/lfd/banner")
@Validated
public class LfdBannerController {

    @Resource
    private LfdBannerService bannerService;

    @PostMapping("/create")
    @Operation(summary = "创建Banner")
    @PreAuthorize("@ss.hasPermission('lfd:banner:create')")
    public CommonResult<Long> createBanner(@Valid @RequestBody LfdBannerCreateReqVO createReqVO) {
        return success(bannerService.createBanner(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Banner")
    @PreAuthorize("@ss.hasPermission('lfd:banner:update')")
    public CommonResult<Boolean> updateBanner(@Valid @RequestBody LfdBannerUpdateReqVO updateReqVO) {
        bannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Banner")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:banner:delete')")
    public CommonResult<Boolean> deleteBanner(@RequestParam("id") Long id) {
        bannerService.deleteBanner(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Banner")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:banner:query')")
    public CommonResult<LfdBannerRespVO> getBanner(@RequestParam("id") Long id) {
        LfdBannerDO banner = bannerService.getBanner(id);
        return success(LfdBannerConvert.INSTANCE.convert(banner));
    }

    @GetMapping("/list")
    @Operation(summary = "获得Banner列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:banner:query')")
    public CommonResult<List<LfdBannerRespVO>> getBannerList(@RequestParam("ids") Collection<Long> ids) {
        List<LfdBannerDO> list = bannerService.getBannerList(ids);
        return success(LfdBannerConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Banner分页")
    @PreAuthorize("@ss.hasPermission('lfd:banner:query')")
    public CommonResult<PageResult<LfdBannerRespVO>> getBannerPage(@Valid LfdBannerPageReqVO pageVO) {
        PageResult<LfdBannerDO> pageResult = bannerService.getBannerPage(pageVO);
        return success(LfdBannerConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Banner Excel")
    @PreAuthorize("@ss.hasPermission('lfd:banner:export')")
    @OperateLog(type = EXPORT)
    public void exportBannerExcel(@Valid LfdBannerExportReqVO exportReqVO,
                                  HttpServletResponse response) throws IOException {
        List<LfdBannerDO> list = bannerService.getBannerList(exportReqVO);
        // 导出 Excel
        List<LfdBannerExcelVO> datas = LfdBannerConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "Banner.xls", "数据", LfdBannerExcelVO.class, datas);
    }

    @GetMapping("/mini-list")
    @Operation(summary = "banner")
    @PermitAll
    public CommonResult<List<String>> miniList() {
        LfdBannerExportReqVO exportReqVO=new LfdBannerExportReqVO();
        List<LfdBannerDO> list = bannerService.getBannerList(exportReqVO);
        return success(CollectionUtil.map(list,LfdBannerDO::getPhoto,true));
    }
}
